/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.FreshAirVentilatorLog;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.FreshAirVentilatorLogRepository;
import me.zhengjie.modules.biz.service.FreshAirVentilatorLogService;
import me.zhengjie.modules.biz.service.dto.FreshAirVentilatorLogDto;
import me.zhengjie.modules.biz.service.dto.FreshAirVentilatorLogQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.FreshAirVentilatorLogMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class FreshAirVentilatorLogServiceImpl implements FreshAirVentilatorLogService {

    private final FreshAirVentilatorLogRepository freshAirVentilatorLogRepository;
    private final FreshAirVentilatorLogMapper freshAirVentilatorLogMapper;

    @Override
    public Map<String,Object> queryAll(FreshAirVentilatorLogQueryCriteria criteria, Pageable pageable){
        Page<FreshAirVentilatorLog> page = freshAirVentilatorLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(freshAirVentilatorLogMapper::toDto));
    }

    @Override
    public List<FreshAirVentilatorLogDto> queryAll(FreshAirVentilatorLogQueryCriteria criteria){
        return freshAirVentilatorLogMapper.toDto(freshAirVentilatorLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FreshAirVentilatorLogDto findById(Integer id) {
        FreshAirVentilatorLog freshAirVentilatorLog = freshAirVentilatorLogRepository.findById(id).orElseGet(FreshAirVentilatorLog::new);
        ValidationUtil.isNull(freshAirVentilatorLog.getId(),"FreshAirVentilatorLog","id",id);
        return freshAirVentilatorLogMapper.toDto(freshAirVentilatorLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FreshAirVentilatorLogDto create(FreshAirVentilatorLog resources) {
        return freshAirVentilatorLogMapper.toDto(freshAirVentilatorLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FreshAirVentilatorLog resources) {
        FreshAirVentilatorLog freshAirVentilatorLog = freshAirVentilatorLogRepository.findById(resources.getId()).orElseGet(FreshAirVentilatorLog::new);
        ValidationUtil.isNull( freshAirVentilatorLog.getId(),"FreshAirVentilatorLog","id",resources.getId());
        freshAirVentilatorLog.copy(resources);
        freshAirVentilatorLogRepository.save(freshAirVentilatorLog);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            freshAirVentilatorLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FreshAirVentilatorLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FreshAirVentilatorLogDto freshAirVentilatorLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", freshAirVentilatorLog.getName());
            map.put("楼层", freshAirVentilatorLog.getFloor());
            if (freshAirVentilatorLog.getIndoorTemperature() != 0) {
                map.put("室内温度", freshAirVentilatorLog.getIndoorTemperature() + "℃");
            } else {
                map.put("室内温度", "-");
            }
            if (freshAirVentilatorLog.getAmbientTemperature() != 0) {
                map.put("环境温度", freshAirVentilatorLog.getAmbientTemperature() + "℃");

            } else {
                map.put("环境温度", "-");
            }
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Scheduled(cron = "0 0 */1 * * ?")
    public void deleteLog() {
        long time = System.currentTimeMillis()/1000-30*24*60;
        freshAirVentilatorLogRepository.deleteLogs(time);

    }
}
